﻿using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using System;
using System.Collections.Generic;

//先选择元素后执行命令
//可以点选多选框选
namespace HelloRevit
{
    [Transaction(TransactionMode.Manual)]
    public class Doc_Selection : IExternalCommand
    {
        public Result Execute(ExternalCommandData commandData,ref string message, ElementSet elements)
        {
            try
            {

                //界面交互的DOC
                UIDocument uidoc = commandData.Application.ActiveUIDocument;
                //实际内容的DOC
                Document doc = uidoc.Document;

                // 取到当前文档的选择集.
                Selection selection = uidoc.Selection;
                ICollection<ElementId> selectedIds = uidoc.Selection.GetElementIds();

               
                if (0 == selectedIds.Count)
                {
                    // If no elements selected.
                    TaskDialog.Show("RevitError", "你没有选择元素.");
                }
                else
                {
                    String info = "所选择元素类型: ";
                    foreach (ElementId id in selectedIds)
                    {
                        Element elem = doc.GetElement(id);
                        if (elem != null )
                        {
                            info += "\n\t" + elem.GetType().ToString();
                        }
   
                    }

                    TaskDialog.Show("RevitSucc", info);
                }
            }
            catch (Exception e)
            {
                message = e.Message;
                return Result.Failed;
            }

            return Result.Succeeded;
        }
        /// </ExampleMethod>
    }
}
